﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using MovieRental.Entities;

namespace MovieRental.EfRepositoryLibrary.Mappers
{
    public class LoanMapper : BaseMapper<LoanInfo, Guid, Loan, Loan, MovieRentalEntitiesContainer>
    {
        public override void Convert(LoanInfo entity, Loan model)
        {
            model.Id = entity.Id;
            model.LoanCartId = entity.LoanCartId;
            model.EditionId = (entity.EditionId == Guid.Empty) ? null : new Nullable<Guid>(entity.EditionId);
            model.PricePerDay = entity.PricePerDay;
            model.ReturnDate = (entity.ReturnDate == DateTime.MinValue) ? null : new Nullable<DateTime>(entity.ReturnDate);
        }

        public override Guid SelectKey(Loan model)
        {
            return model.Id;
        }

        public override Guid SelectKey(LoanInfo entity)
        {
            return entity.Id;
        }

        public override ObjectSet<Loan> SelectObjectSet(MovieRentalEntitiesContainer context)
        {
            return context.Loans;
        }

        public override void ConvertBack(Loan model, LoanInfo entity)
        {
            entity.Id = model.Id;
            entity.LoanCartId = model.LoanCartId;
            entity.EditionId = (model.EditionId == null) ? Guid.Empty : model.EditionId.Value;
            entity.PricePerDay = model.PricePerDay;
            entity.ReturnDate = (model.ReturnDate == null) ? DateTime.MinValue : model.ReturnDate.Value;
        }
    }
}
